Introduction
Welcome!
Code Examples
Day 1
Challenge: KISS
Solution: KISS
Day 2
Challenge: Type Annotations
Solution: Type Annotations
Day 3
Challenge: Decoupling
Solution: Decoupling
Day 4
Challenge: DRY
Solution: DRY
Day 5
Challenge: String Formatting
Solution: String Formatting
Day 6
Challenge: Law Of Demeter
Solution: Law Of Demeter
Day 7
Challenge: Better Discounts
Solution: Better Discounts
Day 8
Challenge: Payment Strategy
Solution: Payment Strategy
Day 9
Challenge: Plugins
Solution: Plugins
Day 10
Challenge: Object Oriented To Functional
Solution: Object Oriented To Functional
Day 11
Challenge: Cohesion
Solution: Cohesion
Day 12
Challenge: MVP
Solution: MVP
Day 13
Challenge: Inheritance
Solution: Inheritance
Day 14
Challenge: Abstraction
Solution: Abstraction
Day 15
Challenge: Higher-Order Functions
Solution: Higher-Order Functions
Day 16
Challenge: Configuration
Solution: Configuration
Day 17
Challenge: Concurrency
Solution: Concurrency
Day 18
Challenge: Refactoring
Solution: Refactoring
Day 19
Challenge: Itertools
Solution: Itertools
Day 20
Challenge: Inappropriate Intimacy
Solution: Inappropriate Intimacy
Wrap Up
End of Part 1

from collections import defaultdict def count_fruits(fruits: list[str]) -> dict[str, int]: # your code goes here counter = defaultdict(int) for fruit in fruits: counter[fruit] += 1 return counter
Looks good!
Ended with the following solution:
```
def count_fruits(fruits: list[str]) -> dict[str, int]:
"""
Count the frequency of each type of fruit in the list.
:param fruits: list[str] - A list of fruits.
:return: dict[str, int] - A dictionary with the fruit names as keys and their counts as values.
"""
fruit_count: dict[str, int] = {}
for unique_fruit in set(fruits):
fruit_count[unique_fruit] = fruits.count(unique_fruit)
return fruit_count
```
Thanks for showing the Counter class
Nice solution! nice use of different data structures to speed thing up also ;)
Can't believe I haven't seen the good ol' dict.get() method applied in the comments!
def count_fruits(fruits: list[str]) -> dict[str, int]:
""" Create an empty dictionary. Check if the item exists as a key using the *get* method. If not, create key with 0 as a default value, if it does just add 1 to the value."""
d : dict[str, int] = {}
for item in fruits:
d[item] = d.get(item,0)+1
return d
Nice solution Alexandru!
Can we use this one as an alternative solution in our course repo?
Of course, your code will be acknowledged as a contribution to the course.
If you are interested, contact support@arjancodes.com with your GitHub account name and a link to this conversation
This solution is now added to the GitHub repo under the contributions header in the readme. Thanks again for your contribution!
from collections import defaultdict
def count_fruits(fruits: list[str]) -> dict[str, int]:
# your code goes here
counter = defaultdict(int)
for fruit in fruits:
counter[fruit] += 1
return counter
Looks good! Nice work :)
You can do it in a single line even without the Counter:
def count_fruits_comprehension(fruits: list[str]) -> dict[str, int]:
return {fruit: len([f for f in fruits if f == fruit]) for fruit in set(fruits)}
Agreed! However, it is less readable in my view because of the nested comprehensions.